import Vue from "vue";
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";
import { AuthenticationStore } from "@/store/AuthenticationStore/AuthenticationStore";
import { SkeletonLoaderStore } from "@/store/SkeletonLoaderStore/SkeletonLoaderStore";
import { OverlayStore } from "@/store/OverlayStore/OverlayStore";
import { DashboardStore } from "@/store/DashboardStore/DashboardStore";
import { MaintenanceStore } from "@/store/MaintenanceStore/MaintenanceStore";
import { PendingJobStore } from "@/store/PendingJobStore/PendingJobStore";
import { QuotationStore } from "@/store/QuotationStore/QuotationStore";
import { CustomerStore } from "@/store/CustomerStore/CustomerStore";
import { ContactInfoStore } from "@/store/CustomerStore/ContactInfoStore/ContactInfoStore";
import { ExistingCustomerStore } from "@/store/CustomerStore/ExistingCustomerStore/ExistingCustomerStore";
import { ProspectStore } from "@/store/CustomerStore/ProspectStore/ProspectStore";
import { DialogStore } from "@/store/CustomerStore/DialogStore/DialogStore";
import { LibraryStore } from "@/store/LibraryStore/LibraryStore";
import { KioskStore } from "@/store/KioskStore/KioskStore";
import { ProjectsStore } from "@/store/ProjectsStore/ProjectsStore";
import { InvoiceStore } from "@/store/InvoiceStore/InvoiceStore";

Vue.use(Vuex);

const storeGetterPath = {
  url: "url",
  baseUrl: "baseUrl",
  CompCode: "CompCode",
  sysMsg: "sysMsg",
  t: "t",
  AuthenticationStore: {
    accessToken: "AuthenticationStore/accessToken",
    userID: "AuthenticationStore/userID",
    menuRights: "AuthenticationStore/menuRights",
    getSysParms: "AuthenticationStore/getSysParms",
    centerCode: "AuthenticationStore/centerCode",
    EntNum: "AuthenticationStore/EntNum",
  },
  LoaderStore: {
    show: "LoaderStore/show",
    hide: "LoaderStore/hide",
  },
  DropDownStore: {
    userGrp: "DropDownStore/userGrp",
    userList: "DropDownStore/userList",
    entities: "DropDownStore/entities",
  },
};

const storeCommitPath = {
  setUrl: "setUrl",
  setSysMsg: "setSysMsg",
  setCompCode: "setCompCode",
  setLang: "setLang",
  setDisplayPopup: "setDisplayPopup",
  setMenuSelected: "setMenuSelected",
  setLastVisited: "setLastVisited",
  AuthenticationStore: {
    setIsLoggedIn: "AuthenticationStore/setIsLoggedIn",
    setUserData: "AuthenticationStore/setUserData",
    setRememberMe: "AuthenticationStore/setRememberMe",
    setMenu: "AuthenticationStore/setMenu",
    setProjRights: "AuthenticationStore/setProjRights",
    setSysParam: "AuthenticationStore/setSysParam",
    removeUserData: "AuthenticationStore/removeUserData",
    setCompCode: "AuthenticationStore/setCompCode",
  },
  LoaderStore: {
    show: "LoaderStore/show",
    hide: "LoaderStore/hide",
  },
  DropDownStore: {
    setEntities: "DropDownStore/setEntities",
  },
};

const menuList = [
  {
    id: "01000000",
    title: "DashBoard",
    icon: "mdi-view-grid",
    childrenNames: [],
  },
  {
    id: "02000000",
    title: "Pending Job",
    icon: "mdi-format-list-bulleted-square",
    childrenNames: [],
  },
  {
    id: "04000000",
    title: "Customer",
    icon: "mdi-account",
    childrenNames: [],
  },
  {
    id: "03000000",
    title: "Quotation",
    icon: "mdi-currency-usd",
    childrenNames: [],
  },
  {
    id: "07000000",
    title: "Project",
    icon: "mdi-share-variant",
    childrenNames: [],
  },
  {
    id: "08000000",
    title: "Invoice",
    icon: "mdi-receipt-text",
    childrenNames: [],
  },
  {
    id: "21000000",
    title: "Kiosk",
    icon: "mdi-format-list-checks",
    childrenNames: [],
  },
  {
    id: "13000000",
    title: "Library",
    icon: "mdi-folder",
    childrenNames: [],
  },
  {
    id: "11000000",
    title: "Job Sheet",
    icon: "mdi-file-document",
    childrenNames: [],
  },
  {
    id: "10000000",
    title: "Maintenance",
    icon: "mdi-cog",
    childrenNames: [],
  },
];

const store = new Vuex.Store({
  state: {
    baseUrl: null,
    url: null,
    compCode: "MASTER",
    sysMsg: [],
    Lang: "EN",
    darkMode: false,

    displayPopup: false,
    menuSelected: "",
    menuVisible: true,
    selectedSearch: null,
    searchItems: [
      "Pending Job",
      "Customer",
      "Quotation",
      "Project",
      "Library",
      "Job Sheet",
      "Maintenance",
    ],
    menuList,
  },
  getters: {
    baseUrl: (state: any) => {
      return state.baseUrl;
    },
    url: (state: any) => {
      return state.url;
    },
    sysMsg: (state: any) => {
      return state.sysMsg;
    },
    t: (state: any) => (langConst: any) => {
      if (state.sysMsg) {
        const Msg = state.sysMsg.find(
          (x: any) =>
            x.FieldType == langConst.FieldType &&
            x.FieldName == langConst.FieldName
        );
        if (Msg) {
          return Msg.MsgString;
        } else {
          return langConst.FieldType + "_" + langConst.FieldName;
        }
      } else {
        return langConst.FieldType + "_" + langConst.FieldName;
      }
    },
  },
  mutations: {
    setSelectedSearch(
      state: { selectedSearch: string | null },
      selectedSearch: string
    ) {
      state.selectedSearch = selectedSearch;
    },
    setUrl: (state: any, resp: any) => {
      state.baseUrl = resp;
      state.url = resp + "webapi/";
    },
    setLang: (state: any, resp: any) => {
      state.Lang = resp;
    },
    setCompCode: (state: any, resp: any) => {
      state.compCode = resp;
    },
    setSearchItems(state: any, searchItems) {
      state.searchItems = searchItems;
    },
  },
  actions: {},
  modules: {
    AuthenticationStore,
    SkeletonLoaderStore,
    OverlayStore,
    DashboardStore,
    MaintenanceStore,
    PendingJobStore,
    QuotationStore,
    CustomerStore,
    ContactInfoStore,
    ExistingCustomerStore,
    DialogStore,
    ProspectStore,
    LibraryStore,
    KioskStore,
    ProjectsStore,
    InvoiceStore,
  },
  plugins: [
    createPersistedState({
      paths: ["AuthenticationStore", "LoaderStore"], //Stores that will kept in location storage after F5 refresh
    }),
  ],
});

export { storeGetterPath, storeCommitPath, store };
